// pages/my/order-detail/index.js
import {
    _headerPost,
    _headerGet,
    config
} from '../../../../utils/httpUtils.js'
const RSA = require("../../../../utils/wx_rsa.js");
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '订单详情' //导航栏 中间的标题
        },
        height: app.globalData.height * 2 + 20,
        ip: config.host,
        modal: true,
        value: 0
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getDetail(options.orderId)
    },
    //获取订单详情
    getDetail(id) {
        _headerGet('/api/order/order_detail', {
            id: id,
            c_client: "weixinSmall"
        }, res => {
            if (res.data.code == 1) {
                this.setData({
                    orderInfo: res.data.data,
                    orderState: this.isState(res.data.data)
                })
            }
        })
    },
    //判断状态
    isState(body) {
        if (body.o_type == 'goods')
            switch (body.o_state) {
                case 0:
                    return 0;
                    break
                case 1:
                    if (body.oe_id == 0)
                        return 1
                    else return 2
                    break
                case 2:
                    break
                case 3:
                    break
                case 4:
                    if (body.orec_id > 0 && body.oref_state > 0) { // 查看详情
                        return 6
                    } else if (body.orec_id == 0 && body.oref_state > 0) { // 评价
                        return 3
                    } else if (body.orec_id > 0 && body.oref_state == 0) { // 售后
                        return 4
                    } else if (body.orec_id == 0 && body.oref_state == 0) { // 可售后，可评价
                        return 5
                    }
                    break
                case 5:
                    return 6
                    break
            } else {
                return 7
            }
    },
    //去支付
    goToPay() {
        let _data = {
            order_id: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${this.data.orderInfo.o_id}`)),
            c_client: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt("weixinSmall")),
            type: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt("order"))
        }
        _headerPost('/api/Order/commit_order', _data, res => {
            if (res.data.code == 1) {
                wx.navigateTo({
                    url: '/pages/public/topay/index?payInfo=' + JSON.stringify(res.data.data)
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
            }
        })

    },

    //删除订单
    delOrder() {
        _headerPost('/api/order/order_del', {
            order_id: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${this.data.orderInfo.o_id}`)),
            c_client: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt("weixinSmall")),
        }, res => {
            if (res.data.code == 1) {
                wx.showToast({
                    title: res.data.msg,
                })
                wx.navigateBack({})
            } else {
                wx.showToast({
                    title: res.data.msg,
                })
            }
        })
    },
    //追加评价
    Evaluation(e) {
        let index = e.currentTarget.id
        let data = this.data.orderInfo.o_goods[index]
        data.shopId = this.data.orderInfo.shop_id
        data.oId = this.data.orderInfo.o_id
        wx.navigateTo({
            url: '../evaluate/index?data=' + JSON.stringify(data),
        })
    },
    //评价
    Evaluate() {
        let data = this.data.orderInfo.o_goods[0]
        data.shopId = this.data.orderInfo.shop_id
        data.oId = this.data.orderInfo.o_id
        wx.navigateTo({
            url: '../evaluate/index?data=' + JSON.stringify(data),
        })
    },
    //查看物流
    lockExpress() {
        wx.navigateTo({
            url: `/pages/public/logistics/index?id=${this.data.orderInfo.o_id}`,
        })
    },
    //提醒发货
    remindDeliver() {
        wx.showToast({
            title: '提醒成功',
        })
    },
    confirmGoods() {
        //确认收货
        wx.showModal({
            title: '提示',
            content: '您确定要确认收货？',
            success: res => {
                if (res.confirm) {
                    _headerPost('/api/order/order_receiving', {
                        id: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt(`${this.data.orderInfo.o_id}`)),
                        c_client: RSA.hex2b64(RSA.KEYUTIL.getKey(app.globalData.publicKey).encrypt("weixinSmall"))
                    }, res => {
                        if (res.data.code == 1) {
                            wx.showModal({
                                title: '提示',
                                content: '收货成功',
                                showCancel: false
                            })
                            wx.navigateBack({})
                        } else {
                            wx.showToast({
                                title: res.data.msg,
                                icon: 'none'
                            })
                        }
                    })
                }
            }
        })
    },
    //评价分数
    sliderChange(e) {
        console.log(e.detail.value)
        this.setData({
            slider: e.detail.value,
        })
    }
})